

package com.hazelcast.internal.metrics.impl;

import com.hazelcast.internal.metrics.MetricDescriptor;
import com.hazelcast.internal.metrics.collectors.MetricsCollector;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import static com.hazelcast.internal.metrics.impl.DefaultMetricDescriptorSupplier.DEFAULT_DESCRIPTOR_SUPPLIER;
import static java.util.Collections.unmodifiableList;
import static java.util.Collections.unmodifiableMap;

public class CapturingCollector implements MetricsCollector {

    private final Map<MetricDescriptor, Capture> captures = new HashMap<>();

    @Override
    public void collectLong(MetricDescriptor descriptor, long value) {
        captures.computeIfAbsent(DEFAULT_DESCRIPTOR_SUPPLIER.get().copy(descriptor), d -> new Capture()).capture(value);
    }

    @Override
    public void collectDouble(MetricDescriptor descriptor, double value) {
        captures.computeIfAbsent(DEFAULT_DESCRIPTOR_SUPPLIER.get().copy(descriptor), d -> new Capture()).capture(value);
    }

    @Override
    public void collectException(MetricDescriptor descriptor, Exception e) {
        // nop
    }

    @Override
    public void collectNoValue(MetricDescriptor descriptor) {
        // nop
    }

    public Map<MetricDescriptor, Capture> captures() {
        return unmodifiableMap(captures);
    }

    public boolean isCaptured(MetricDescriptor descriptor) {
        return captures.containsKey(descriptor);
    }

    public static final class Capture {
        private final List<Number> values = new LinkedList<>();
        private int hits = 0;

        private void capture(Number value) {
            values.add(value);
            hits++;
        }

        public int hits() {
            return hits;
        }

        public Number singleCapturedValue() {
            if (values.size() != 1) {
                throw new AssertionError("Expected to have only 1 value captured, but we captured " + values.size() + " " + "values");
            }
            return values.get(0);
        }

        public List<Number> values() {
            return unmodifiableList(values);
        }

        @Override
        public String toString() {
            return "Capture{" + "values=" + values + ", hits=" + hits + '}';
        }
    }
}
